//
//  UserAccount.swift
//  PersonnalDiary
//
//  Created by 徐宽阔 on 15/10/1.
//  Copyright © 2015年 XDKOO. All rights reserved.
//

import UIKit

class UserAccount: NSObject ,NSCoding{
    
    // MARK: - 属性列表
    var access_token:String?
    
    var expires_in:NSNumber?
        {
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in!.doubleValue)
        }
    }
    
    var uid:String?
    
    // 添加一个确切的时间属性
    var expires_date: NSDate?
    /// 用户头像
    var avatar_large: String?
    /// 用户昵称
    var screen_name: String?
    
    // MARK: - 账号模型
    
    init(dict: [String: AnyObject])
    {
        // 必须调用父类初始化分配存储空间
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    static let keys = ["access_token", "expires_in", "uid"]
    override var description: String {
        return "\(dictionaryWithValuesForKeys(UserAccount.keys))"
    }
    
    
    // MARK: - 序列化
    func encodeWithCoder(aCoder: NSCoder)
    {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    required init?(coder aDecoder: NSCoder)
    {
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeObjectForKey("expires_in") as? NSNumber
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
    
    
    // MARK: - 存储账号
    
    private static let path = "account.plist".cacheDir()
    
    static var account: UserAccount?
    
    func saveAccount()
    {
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.path)
    }
    
    class func loadAccount() -> UserAccount? {
        if account == nil
        {
            account = NSKeyedUnarchiver.unarchiveObjectWithFile(UserAccount.path) as? UserAccount
        }
        
        if account != nil
        {
            if account!.expires_date!.compare(NSDate()) == NSComparisonResult.OrderedAscending
            {
                account = nil
            }
        }
        return account
    }
    
    // MARK: - 获取账号信息
    func loadUserInfo(finished: (account: UserAccount?, error: NSError?) -> ())
    {
        let path = "2/users/show.json"
        // 2.定义参数
        let parames = ["access_token": access_token!, "uid":uid!]
        
        NetworkTools.shareNetworkTools().GET(path, parameters: parames, success: { (_, dict) -> Void in
            self.avatar_large = dict["avatar_large"] as? String
            self.screen_name = dict["screen_name"] as? String
            finished(account:self, error: nil)
            }) { (_, error) -> Void in
                finished(account: nil, error:error)
        }
    }
    
    
    
}
